/**
 * Created by FDG282 on 2017/9/1.
 */
$(function () {
    //登录框切换
    $(".login_list_btn .link-text").on('click',function(){
        var linkindex = $(this).index();
        $(this).addClass("a-active").siblings(".link-text").removeClass("a-active");
        $(".tab1").eq(linkindex).removeClass("hide").addClass("show").siblings(".tab1").addClass("hide");
        $(".user_login_btn .btn").eq(linkindex).removeClass("hide").addClass("show").siblings(".btn").addClass("hide");
    });
    //表单验证
    var info = "<span class='info'></span>";
    var inputBg = '<span class="inpdel">X</span>';
    var verificationInfo = {
        phoneInfo: {
            errMsg1: "手机号不能为空！",
            errMsg2: "手机号格式不正确！"
        },
        passwordInfo: {
            errMsg1: "密码不能为空！",
            errMsg2: "密码长度至少6个字符必须含有字母！"
        },
        verification: {
            errMsg1: "验证码不能为空！",
            errMsg2: "验证码格式错误！"
        },
        passwordAginInfo: {
            errMsg1:"请重复密码",
            errMsg2: "两次输入的密码不一致"
        },
        winInfo: "注册成功,获得积分,请登陆！"
    };
    $('input[type="text"],input[type="password"]').each(function(){
        $(this).focus(function(){
            if($(this).parents(".input-box").find('span.inpdel').length<=0){
                $(this).parents(".input-box").append(inputBg);
            }
            $(".inpdel").on('click',function(){
                $(this).siblings('input').val('').focus()
            });
        });
    });
    //登录验证
    $(".form_phone_group, .form_password_group,.img_verification,.phone_verification,.form_passwordsure_group").append(info);
    var wrap1 = $("#form_1");
    var wrap2 = $("#form_2");
    var wrap3= $("#register_form");
    var phoneInpt = $(".js_phone");
    var passwordInpt = $(".js_password");
    var passwordSure = $(".js_passwrod_sure");
    var imgCode = $(".js_img_code");
    var phoneCode = $(".js_phone_code");
    var logUseInfo = $(".form_phone_group .info");
    var logPasInfo = $(".form_password_group .info");
    var pasSureInfo = $(".form_passwordsure_group .info");
    var imgCodeInfo = $(".img_verification .info");
    var phoneCodeInfo = $(".phone_verification .info");
    $(".form_input_group.info").empty();
    phoneInpt.keyup(function () {
        var userphone = $(this).val();
        if (userphone <= 0) {
            logUseInfo.html(verificationInfo.phoneInfo.errMsg1);
            logUseInfo.addClass("infoerror");
            $(this).removeClass("infowaring")
        } else if (!(/^1[3|4|5|8][0-9]\d{8,11}$/.test(userphone))) {
            logUseInfo.html(verificationInfo.phoneInfo.errMsg2);
            logUseInfo.addClass("infoerror");
            $(this).removeClass("infowaring")
        } else {
            logUseInfo.html('');
            $(this).addClass("infowaring");
        }
    });
    phoneInpt.blur(function () {
        var username = phoneInpt.val();
        if (username.length <= 0) {
            phoneInpt.addClass("inputerror");
            logUseInfo.html(verificationInfo.phoneInfo.errMsg1);
            logUseInfo.addClass("infoerror")
        }
    });
    passwordInpt.keyup(function () {
        var password = $(this).val();
        if (password.length <= 0) {
            logPasInfo.html(verificationInfo.passwordInfo.errMsg1);
            logPasInfo.addClass("infoerror");
            $(this).removeClass("infowaring")
        } else if (!(/([a-zA-Z0-9!@#$%^&*()_?<>{}]){6,16}/.test(password))) {
            logPasInfo.html(verificationInfo.passwordInfo.errMsg2);
            logPasInfo.addClass("infoerror");
            $(this).removeClass("infowaring")
        } else {
            logPasInfo.html('');
            $(this).addClass("infowaring")
        }
    });
    imgCode.keyup(function(){
        var imgCodeVal = $(this).val();
        if(imgCodeVal.length>=4){
            imgCodeInfo.html('');
            $(this).addClass("infowaring")
        }else{
            imgCodeInfo.html(verificationInfo.verification.errMsg2);
            imgCodeInfo.addClass("infoerror");
            $(this).removeClass("infowaring")
        }
    });
    phoneCode.keyup(function(){
        var phoneCodeVal = $(this).val();
        if(phoneCodeVal.length>=6){
            phoneCodeInfo.html('');
            $(this).addClass("infowaring")
        }else{
            phoneCodeInfo.html(verificationInfo.verification.errMsg2);
            phoneCodeInfo.addClass("infoerror");
            $(this).removeClass("infowaring")
        }
    });
    //密码登录
    $(".js_login_btn1").click(function () {
        var phoneVal = wrap1.find(phoneInpt).val();
        var passwordVal = wrap1.find(passwordInpt).val();
        var phoneinfoShow = wrap1.find(logUseInfo);
        var phoneinfoBox = wrap1.find(phoneInpt);
        var passwordinfoShow = wrap1.find(logPasInfo);
        var passwordinfoBox = wrap1.find(passwordInpt);
        if (phoneVal !== "" && passwordVal !== "") {
            window.location.href='./index.html';
            // ajax回传匹配
        } else if (phoneVal == "") {
            phoneinfoShow.html(verificationInfo.phoneInfo.errMsg1);
            phoneinfoShow.addClass("infoerror");
            phoneinfoBox.removeClass("infowaring").addClass("inputerror")
        } else if (passwordVal == "") {
            passwordinfoBox.removeClass("infowaring");
            passwordinfoShow.html(verificationInfo.phoneInfo.errMsg2);
            passwordinfoShow.addClass("infoerror").addClass("inputerror")
        }
    });
    //手机号码验证登录
    $(".js_login_btn2").click(function(){
        var phoneVal = wrap2.find(phoneInpt).val();
        var imgCodeVal =wrap2.find(imgCode).val();
        var phoneCodeVal = wrap2.find(phoneCode).val();
        var phoneinfoShow = wrap2.find(logUseInfo);
        var phoneinfoBox = wrap2.find(phoneInpt);
        var imgCodeinfoShow = wrap2.find(imgCodeInfo);
        var imgCodeinfoBox = wrap2.find(imgCode);
        var phoneCodeinfoShow = wrap2.find(phoneCodeInfo);
        var phoneCodeinfoBox = wrap1.find(phoneCode);
        if (phoneVal !== "" && imgCodeVal !== ""&&phoneCodeVal!=="") {
            window.location.href='./index.html';
            // ajax回传匹配
        }else if(phoneVal == ""){
            phoneinfoShow.html(verificationInfo.phoneInfo.errMsg1);
            phoneinfoShow.addClass("infoerror");
            phoneinfoBox.removeClass("infowaring").addClass("inputerror")
        }else if(imgCodeVal==""){
            imgCodeinfoShow.html(verificationInfo.verification.errMsg1);
            imgCodeinfoShow.addClass("infoerror");
            imgCodeinfoBox.removeClass("infowaring").addClass("inputerror")
        }else if(phoneCodeVal==""){
            phoneCodeinfoShow.html(verificationInfo.verification.errMsg1);
            phoneCodeinfoShow.addClass("infoerror");
            phoneCodeinfoBox.removeClass("infowaring").addClass("inputerror")
        }
    });
    //倒计时发送短信
    $(".phoneV_btn").click(function(){
        var that = $(this);
        var time = 60;
        //连接接口发送短信
        var tim = setInterval(function(){
            that.text(time+"S后重新发送").attr("disabled",true).addClass("btn-active");
            time--;
            if(time==0){
                clearInterval(tim);
                that.text("获取手机验证码").attr("disabled",false).removeClass("btn-active");
            }
        },1000);

    });

    //注册验证
    $(".js_password_sure").keyup(function(){
        var phoneVal = wrap3.find(passwordInpt).val();
        var sureVal =($(this).val()).toString();
        if(sureVal!=="" && sureVal==phoneVal){
            pasSureInfo.html('');
            $(this).addClass("infowaring")
        }else if(sureVal!=phoneVal){
            pasSureInfo.html(verificationInfo.passwordAginInfo.errMsg2);
            pasSureInfo.addClass("infoerror");
            $(this).removeClass("infowaring")
        }else if(sureVal==""){
            pasSureInfo.html(verificationInfo.passwordAginInfo.errMsg1);
            pasSureInfo.addClass("infoerror");
            $(this).removeClass("infowaring")
        }
    });
    $(".js-register-btn").click(function () {
        var phoneVal = wrap3.find(phoneInpt).val();
        var passwordVal = wrap3.find(passwordInpt).val();
        var passwordSureVal = wrap3.find(passwordInpt).val();
        var imgCodeVal =wrap3.find(imgCode).val();
        var phoneCodeVal = wrap3.find(phoneCode).val();
        var phoneinfoShow = wrap3.find(logUseInfo);
        var phoneinfoBox = wrap3.find(phoneInpt);
        var passwordinfoShow = wrap3.find(logPasInfo);
        var passwordinfoBox = wrap3.find(passwordInpt);
        var imgCodeinfoShow = wrap3.find(imgCodeInfo);
        var imgCodeinfoBox = wrap3.find(imgCode);
        var phoneCodeinfoShow = wrap3.find(phoneCodeInfo);
        var phoneCodeinfoBox = wrap3.find(phoneCode);
        if (phoneVal !== ""&&passwordVal!=="" && imgCodeVal !== ""&&phoneCodeVal!=="") {
            // ajax回传匹配
            $(".tips").fadeIn(100).children().text(verificationInfo.winInfo);
            window.location.href='./login.html';
        }else if(phoneVal == ""){
            phoneinfoShow.html(verificationInfo.phoneInfo.errMsg1);
            phoneinfoShow.addClass("infoerror");
            phoneinfoBox.removeClass("infowaring").addClass("inputerror")
        }else if(imgCodeVal==""){
            imgCodeinfoShow.html(verificationInfo.verification.errMsg1);
            imgCodeinfoShow.addClass("infoerror");
            imgCodeinfoBox.removeClass("infowaring").addClass("inputerror")
        }else if(phoneCodeVal==""){
            phoneCodeinfoShow.html(verificationInfo.verification.errMsg1);
            phoneCodeinfoShow.addClass("infoerror");
            phoneCodeinfoBox.removeClass("infowaring").addClass("inputerror")
        }else if(passwordVal==""){
            passwordinfoBox.removeClass("infowaring");
            passwordinfoShow.html(verificationInfo.passwordInfo.errMsg2);
            passwordinfoShow.addClass("infoerror").addClass("inputerror")
        }

    });


});